package com.demo2do.boyle.common.handler.impl;

import java.io.IOException;
import java.util.Map;

import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.demo2do.boyle.common.entity.protocol.sms.SmsType;
import com.demo2do.boyle.common.handler.SmsHandler;
import com.demo2do.core.entity.Result;
import com.demo2do.core.utils.SignatureUtils;

/**
 * 
 * @author Wilson
 */
@Component("smsHandler")
public class SmsHandlerImpl implements SmsHandler {

    private static final Logger logger = LoggerFactory.getLogger(SmsHandlerImpl.class);
    
    private String url;

    private String password;
    
    private String message;
    
    public String getUrl() {
		return url;
	}

	public String getPassword() {
		return password;
	}

	public String getMessage() {
		return message;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	/*
     * (non-Javadoc)
     * @see com.demo2do.boyle.client.handler.SmsHandler#sendValidateCode(java.lang.String, java.lang.String, com.demo2do.boyle.client.entity.protocol.sms.SmsType)
     */
    @SuppressWarnings("unchecked")
    public Result sendValidateCode(String phone, String validateCode, SmsType smsType) {

        Result result = new Result();
        String mobile = phone;
        String type = Integer.toString(smsType.ordinal());
        String random = validateCode;
        String sign = SignatureUtils.makeMD5Signature(mobile, this.password, random, type);

        try {
            Response response = Jsoup.connect(this.url)
                .ignoreContentType(true)
                .header("mobile", mobile)
                .header("password", password)
                .data("type", type)
                .data("random", random)
                .data("sign", sign)
                .method(Method.POST)
                .timeout(60000).execute();

            logger.debug(" this.url =============================>: " + this.url);
            
            logger.debug("response body: " + response.body());
            Map<String, Object> map = JSON.parseObject(response.body(), Map.class);
            if("0".equals(map.get("statusCode").toString())) {
                logger.error("sendValidateCode() - result success: " + map.get("message"));
                result.success().message((String)map.get("message"));
            } else {
                logger.error("sendValidateCode() - result fail: " + map.get("message"));
                result.fail().message((String)map.get("message"));
            }

        } catch (IOException e) {
            logger.error("sendValidateCode()", e);
            result.fail().message("服务器异常");
        }

        return result;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.boyle.client.handler.SmsHandler#sendValidateCode(java.lang.String, java.lang.String, com.demo2do.boyle.client.entity.protocol.sms.SmsType)
     */
    /*@SuppressWarnings("unchecked")
    public Result sendTipsText(String mobile) {

        Result result = new Result();
        
        String sign = SignatureUtils.makeMD5Signature(mobile, this.password, "您好，您的微信（微博）账号截图上传错误，请进入“个人中心”->“社交账号”下按要求重新上传，以便尽快完成审核并领取任务。");

        try {
            Response response = Jsoup.connect("http://192.168.1.109/sms/textapi.html")
                .ignoreContentType(true)
                .header("mobile", "15692131937")
                .header("password", this.password)
                .data("text", "您好，您的微信（微博）账号截图上传错误，请进入“个人中心”->“社交账号”下按要求重新上传，以便尽快完成审核并领取任务。")
                .data("sign", sign)
                .method(Method.POST)
                .timeout(60000).execute();

            logger.debug(" this.url =============================>: " + this.url);
            
            logger.debug("response body: " + response.body());
            Map<String, Object> map = JSON.parseObject(response.body(), Map.class);
            if("0".equals(map.get("statusCode").toString())) {
                logger.error("sendValidateCode() - result success: " + map.get("message"));
                result.success().message((String)map.get("message"));
            } else {
                logger.error("sendValidateCode() - result fail: " + map.get("message"));
                result.fail().message((String)map.get("message"));
            }

        } catch (IOException e) {
            logger.error("sendValidateCode()", e);
            result.fail().message("服务器异常");
        }

        return result;
    }*/

}
